﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BCMS.Business.Implementation;

namespace BCMS.Forms
{
    public partial class frmUserAuthorization : Form
    {
        int curUserID = 0;
        string userName = "";
        public frmUserAuthorization(int uID, string uName)
        {
            this.Font = SystemFonts.MessageBoxFont;
            curUserID = uID;
            userName = uName;
            InitializeComponent();
        }

        private void frmUserAuthorization_Load(object sender, EventArgs e)
        {
            cbUser.Items.Add(userName);
            cbUser.Enabled = false;
            cbUser.SelectedIndex = 0;

            DALclient dal = new DALclient();
            string roles = dal.getUserById(curUserID).Roles;

            if (roles.Length >= 4)
            {
                chkClaim.Checked = (roles[0] == '1');
                chkRelease.Checked = (roles[1] == '1');
                chkReport.Checked = (roles[2] == '1');
                chkAdmin.Checked = (roles[3] == '1');
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            char[] roles = new char[] { '0', '0', '0', '0' };

            if (chkClaim.Checked) roles[0] = '1';
            if (chkRelease.Checked) roles[1] = '1';
            if (chkReport.Checked) roles[2] = '1';
            if (chkAdmin.Checked) roles[3] = '1';

            try
            {
                BCMS.Business.Implementation.DALclient dal = new BCMS.Business.Implementation.DALclient();
                dal.updateUserRoles(curUserID, new string(roles));
                if (Settings.GlobalVars.activeUserId == curUserID)
                {
                    MessageBox.Show("Quyền sử dụng đã thay đổi. Khởi động lại chương trình.", "Cảnh báo");
                    try
                    {
                        System.Diagnostics.Process.Start(Application.StartupPath + "/BCMS.exe");
                    }
                    catch
                    {
                    }

                    Application.Exit();
                }
                MessageBox.Show("Cập nhật thành công.");
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }

        }
    }
}
